<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\facade\View;
use think\response\Json;

class Ad extends AdminBase
{
    public function lists(): string
    {
        $adPositionBusiness = new \app\common\business\AdPosition($this->websiteCode);
        $adPosition         = $adPositionBusiness->getAllAdPosition(['status' => self::_IS_ENABLE_STATUS]);
        View::assign(['adPosition' => $adPosition]);

        return View::fetch();
    }

    public function list(): Json
    {
        $positionId = $this->request->param('position_id', 0, 'intval');
        $page       = $this->request->param('page', 1, 'intval');
        $limit      = $this->request->param('limit', 10, 'intval');
        $title      = $this->request->param('title', '', 'trim');
        $status     = $this->request->param('status', 0, 'intval');
        $adBusiness = new \app\common\business\Ad($this->websiteCode);

        $where = [];
        if ($title) {
            $where[] = ['title', 'like', '%' . $title . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }
        if ($positionId) {
            $where[] = ['position_id', '=', $positionId];
        }

        $adInfo = $adBusiness->getAdList($this->cdnDomain,$page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $adInfo['rows'], $adInfo['count']);
    }

    public function add(): string
    {
        $adPositionBusiness = new \app\common\business\AdPosition($this->websiteCode);
        $adPosition         = $adPositionBusiness->getAllAdPosition(['status' => self::_IS_ENABLE_STATUS]);
        View::assign(['adPosition' => $adPosition]);
        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $title      = $this->request->param('title', '', 'trim');
        $positionId = $this->request->param('position_id', 0, 'intval');
        $image      = $this->request->param('image', '', 'trim');
        $status     = $this->request->param('status', 0, 'intval');
        $sort       = $this->request->param('sort', 0, 'intval');
        $url        = $this->request->param('url', '', 'trim');
        $desc       = $this->request->param('desc', '', 'trim');

        $data     = [
            'title'       => $title,
            'status'      => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'position_id' => $positionId,
            'image'       => $image,
            'sort'        => $sort,
            'url'         => $url,
            'desc'        => $desc,
        ];
        $validate = (new \app\admin\validate\Ad())->scene('add');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $adBusiness = new \app\common\business\Ad($this->websiteCode);
        $res        = $adBusiness->addAd($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $adPositionBusiness = new \app\common\business\AdPosition($this->websiteCode);
        $adPosition         = $adPositionBusiness->getAllAdPosition();
        View::assign(['adPosition' => $adPosition]);

        $adBusiness = new \app\common\business\Ad($this->websiteCode);
        $ad         = $adBusiness->findAdById($this->cdnDomain,$id);
        if (empty($ad)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(["ad" => $ad]);

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id         = $this->request->param('id', 0, 'intval');
        $title      = $this->request->param('title', '', 'trim');
        $positionId = $this->request->param('position_id', 0, 'intval');
        $image      = $this->request->param('image', '', 'trim');
        $status     = $this->request->param('status', 0, 'intval');
        $sort       = $this->request->param('sort', 0, 'intval');
        $url        = $this->request->param('url', '', 'trim');
        $desc       = $this->request->param('desc', '', 'trim');

        $data     = [
            'title'       => $title,
            'status'      => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'position_id' => $positionId,
            'image'       => $image,
            'sort'        => $sort,
            'url'         => $url,
            'desc'        => $desc,
        ];
        $validate = (new \app\admin\validate\Ad())->scene('edit');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $adBusiness = new \app\common\business\Ad($this->websiteCode);
        $res        = $adBusiness->updateAd($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $adBusiness = new \app\common\business\Ad($this->websiteCode);
        $res        = $adBusiness->deleteAd($id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doSort(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $ids  = $this->request->param('ids', '', 'trim');
        $sort = $this->request->param('sort', '', 'trim');
        if (empty($ids) || empty($sort)) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $idArr = explode(',', $ids);
        foreach ($idArr as $k => $v) {
            $idArr[$k] = intval($v);
        }

        $sortArr = explode(',', $sort);
        foreach ($sortArr as $k => $v) {
            $sortArr[$k] = intval($v);
        }

        $data = [];
        foreach ($idArr as $k => $v) {
            $data[$v] = $sortArr[$k];
        }

        $adBusiness = new \app\common\business\Ad($this->websiteCode);
        $res        = $adBusiness->batchAdSort($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function batchDoDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $ids = $this->request->param("ids");
        if ($ids == "") {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }
        $ids = explode(",", $ids);
        foreach ($ids as $k => $v) {
            $ids[$k] = intval($v);
        }

        $adBusiness = new \app\common\business\Ad($this->websiteCode);
        $res        = $adBusiness->batchDeleteAd($ids);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }
}
